home *** CD-ROM | disk | FTP | other *** search
- // MSAECopy.c
- //
- // Original version by Jon Lansdell and Nigel Humphreys.
- // 4.0 and 3.1 updates by Greg Sutton.
- // ©Apple Computer Inc 1996, all rights reserved.
-
- #include "MSAECopy.h"
-
- #include "MSAEUtils.h"
- #include "MSWindow.h" // for DPtrFromWindowPtr()
-
- #include "MSAESelect.h"
-
- #include <Scrap.h>
-
-
- #pragma segment AppleEvent
-
- // Handle a copy to scrap e.g 'copy last word of document 1'
- // Note that 'copy last word of document 1 to end of document 2' is a kAEClone event
-
- pascal OSErr DoCopy(const AppleEvent *theAppleEvent, AppleEvent *reply, long refcon)
- {
- #ifdef __MWERKS__
- #pragma unused (reply, refcon)
- #endif
-
- AEDesc directObj = {typeNull, NULL};
- TextToken aTextToken;
- short ignore;
- OSErr err;
-
- err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &directObj);
- // If we get an error here it just means that they haven't supplied a reference to
- // an object to copy - so copy the current section instead.
-
- if (directObj.descriptorType != typeNull)
- err = CopyDesc(&directObj);
- else
- { // Just copy the selection of the front window
- err = GetWindowSelection(FrontWindow(), &aTextToken, &ignore);
- if (noErr != err) goto done;
-
- err = CopyTextToken(&aTextToken);
- }
-
- done:
- (void)AEDisposeDesc(&directObj);
-
- return(err);
- } // DoCopy
-
-
- OSErr CopyTextToken(TextToken* theToken)
- {
- WindowPtr aWindow;
- DPtr docPtr;
- OSErr err;
-
- aWindow = theToken->tokenWindow;
- docPtr = DPtrFromWindowPtr(theToken->tokenWindow);
-
- if (! aWindow || ! docPtr)
- return(errAENoSuchObject);
-
- // Set this tokens selection
- err = SelectTextToken(theToken);
- if (noErr != err) goto done;
-
- err = (OSErr)ZeroScrap();
- TECopy(docPtr->theText);
-
- done:
- return(err);
- }
-
- OSErr CopyTextDesc(AEDesc* textDesc)
- {
- TextToken aTextToken;
- Size actualSize;
- OSErr err;
-
- if (typeMyText != textDesc->descriptorType)
- return(errAETypeError);
-
- GetRawDataFromDescriptor(textDesc, (Ptr)&aTextToken, sizeof(aTextToken), &actualSize);
-
- err = CopyTextToken(&aTextToken);
-
- return(err);
- }
-
- OSErr CopyDesc(AEDesc* aDesc)
- {
- AEDesc copyDesc = {typeNull, NULL},
- textDesc = {typeNull, NULL};
- OSErr err;
-
- if (typeObjectSpecifier == aDesc->descriptorType)
- err = AEResolve(aDesc, kAEIDoMinimum, ©Desc);
- else if (typeNull != aDesc->descriptorType)
- err = AEDuplicateDesc(aDesc, ©Desc);
-
- if (noErr != err) goto done;
-
- switch (copyDesc.descriptorType)
- {
- case typeAEList:
- err = errAETypeError;
- // We can't handle copying more than one item to the scrap
- break;
-
- default:
- err = AECoerceDesc(©Desc, typeMyText, &textDesc);
- if (noErr != err) goto done;
- err = CopyTextDesc(&textDesc);
- }
-
- done:
- (void)AEDisposeDesc(©Desc);
- (void)AEDisposeDesc(&textDesc);
-
- return(err);
- }
-